package org.boulderfoodrescue.webapp.client.old.viewer.findaddress;

import org.boulderfoodrescue.webapp.client.multipleaddresspanel.MultipleAdressDialog;
import org.boulderfoodrescue.webapp.client.old.viewer.SideBarPanel;
import org.boulderfoodrescue.webapp.shared.Place;

import com.google.gwt.core.client.GWT;
import com.google.gwt.core.client.JsArray;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.KeyCodes;
import com.google.gwt.event.dom.client.KeyUpEvent;
import com.google.gwt.event.logical.shared.CloseEvent;
import com.google.gwt.event.logical.shared.CloseHandler;
import com.google.gwt.maps.client.geocode.Geocoder;
import com.google.gwt.maps.client.geocode.LocationCallback;
import com.google.gwt.maps.client.geocode.Placemark;
import com.google.gwt.maps.client.geocode.StatusCodes;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.Widget;

public class FindAddressPanel extends Composite implements CloseHandler<Placemark>{

  private static FindAddressPanelUiBinder uiBinder = GWT
      .create(FindAddressPanelUiBinder.class);

  interface FindAddressPanelUiBinder extends
      UiBinder<Widget, FindAddressPanel> {
  }

  public FindAddressPanel(SideBarPanel parent) {
    this.parent = parent;
    initWidget(uiBinder.createAndBindUi(this));
  }
  @UiField
  FlowPanel address_search;
  @UiField
  TextBox address_text_one;
  @UiField
  Button button_find;
  private SideBarPanel parent;
  @UiHandler("address_text_one")
  public void onEnterKeyHit(KeyUpEvent e)
  {
    if (KeyCodes.KEY_ENTER == e.getNativeKeyCode())
      onFindClick(null);
  }
  @UiHandler("button_find")
  public void onFindClick(ClickEvent e){
    final Place place = new Place();
    place.setAddressOne(address_text_one.getText());
    Geocoder gCoder = new Geocoder();
    gCoder.getLocations(place.getAddressOne(), new LocationCallback(){
      @Override
      public void onFailure(int statusCode) {
        String error = "An Unknown Error Occured";
        if(statusCode == StatusCodes.API_ERROR)
          error = "Error inside google api call";
        else if(statusCode == StatusCodes.BAD_KEY)
          error = "Bad Key Error. The given key is either invalid or " +
              "does not match the domain for which it was given.";
        else if(statusCode == StatusCodes.BAD_REQUEST)
          error = "Bad Request Error. A directions request could not be successfully parsed.";
        else if(statusCode == StatusCodes.MISSING_ADDRESS)
          error = "Missing Address Error. The HTTP q parameter was either missing or had no value.";
        else if(statusCode == StatusCodes.MISSING_QUERY)
          error = "Missing Query Error. The HTTP q parameter was either missing or had no value."; 
        else if(statusCode == StatusCodes.SERVER_ERROR)
          error = "Server Error.  A geocoding or directions request could not be successfully processed, yet the exact reason for the failure is not known.";
        else if(statusCode == StatusCodes.TOO_MANY_QUERIES)
          error = "Too Many Queries Error. The given key has gone over the requests limit in the 24 hour period.";
        else if(statusCode == StatusCodes.UNAVAILABLE_ADDRESS)
          error = "Unavailable Address Error. The geocode for the given address or the route for the given directions query cannot be returned due to legal or contractual reasons.";
        Window.alert(error);
      }
      @Override
      public void onSuccess(JsArray<Placemark> locations) {
        if(locations.length() < 1){
          Window.alert("No address found");
        } else if(locations.length() > 1){
//          MultipleAdressDialog dlg = new MultipleAdressDialog(locations, parent);
//          dlg.center();
        } else{
          Placemark location = locations.get(0);
          parent.addressFound(location);
          
        }
      }
    });
  }
  @Override
  public void onClose(CloseEvent<Placemark> event) {
    // TODO Auto-generated method stub
    
  }
}
